package com.example.project.activity.wode;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.project.R;
import com.example.project.activity.kaishi.LoginActivity;
import com.example.project.tool.SharePreferce;

import java.util.Calendar;

public class RemindActivity extends AppCompatActivity implements View.OnClickListener {
    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    private EditText et_text; //文本输出
    private TextView tv_time; //时间显示
    private ImageView ivBack; //返回按钮
    private Button btRemind; //打开or关闭按钮
    private long timeInMillis; //时间毫秒级
    private Boolean isOpen=false; //打开or关闭提示
    private int minutes,seconds; //分，秒
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remind);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        init();
        isOpen = SharePreferce.getInstance(RemindActivity.this).getBoolean("isOpen");
        if(isOpen){
            btRemind.setText("关闭");
        }else {
            btRemind.setText("打开");
        }
    }

    private void init() {
        ivBack=findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        et_text=findViewById(R.id.et_text);
        et_text.setOnClickListener(this);
        tv_time=findViewById(R.id.tv_time);
        tv_time.setOnClickListener(this);
        btRemind=findViewById(R.id.bt_remind);
        btRemind.setOnClickListener(this);
    }

    //设置闹钟
    public void one(View view){
        //获取当前系统的时间
        Calendar calendar= Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        //弹出时间对话框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                //确定选择好的时间
                Calendar c=Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY,i);
                c.set(Calendar.MINUTE,i1);
                timeInMillis = c.getTimeInMillis();
                System.out.println("c.getTimeInMillis():"+timeInMillis);
                //时间一到，执行相对应的操作
                Intent intent=new Intent();
                intent.setAction("com.example.android28_inform.dd");
                PendingIntent pendingIntent=PendingIntent.getBroadcast(RemindActivity.this,0x101,intent,0);
                //设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
            }
        },hour,minute,true);
        timePickerDialog.show();
    }

    //设置周期闹钟
    public void week (){
        //获取当前系统的时间
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        int minute=calendar.get(Calendar.MINUTE);
        //弹出时间对话框
        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                //确定选择好的时间
                Calendar c=Calendar.getInstance();
                System.out.println("选中的时间："+i+":"+i1);
                minutes=i;
                seconds=i1;
                tv_time.setText(i+":"+i1);
                c.set(Calendar.HOUR_OF_DAY,i);
                c.set(Calendar.MINUTE,i1);

                timeInMillis = c.getTimeInMillis();
            }
        },hour,minute,true);
        timePickerDialog.show();
    }

    //取消周期闹钟
    public void weekalarm(){
        if(pendingIntent!=null){
            alarmManager.cancel(pendingIntent);
        }else {
            btRemind.setText("打开");
            isOpen=false;
            SharePreferce.getInstance(RemindActivity.this).setCache("isOpen",isOpen);
        }

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.tv_time:
                 week();
                break;
            case R.id.bt_remind:
                if (!isOpen){
                    System.out.println("输出");
                    //Intent设置要启动的组件，这里启动广播
                    Intent intent=new Intent();
                    intent.setAction("com.e_eduspace.TIMER_ACTION");
                    //高版本广播设置
                    intent.setComponent(new ComponentName("com.example.project","com.example.project.activity.wode.AlarmReceiver"));
                    //PendingIntent对象设置动作,启动的是Activity还是Service,或广播!
                    pendingIntent = PendingIntent.getBroadcast(RemindActivity.this, 0x102, intent, 0);
                    //设置闹钟(周期)
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,timeInMillis, 86400000,pendingIntent);
                    isOpen=true;
                    btRemind.setText("关闭");
                    Toast.makeText(RemindActivity.this,"打开成功",Toast.LENGTH_SHORT).show();
                    String reminder=et_text.getText().toString().trim();
                    SharePreferce.getInstance(RemindActivity.this).setCache("reminder",reminder);
                    SharePreferce.getInstance(RemindActivity.this).setCache("isOpen",isOpen);
                }else {
                    weekalarm();
                    isOpen=false;
                    btRemind.setText("打开");
                    Toast.makeText(RemindActivity.this,"关闭成功",Toast.LENGTH_SHORT).show();
                    SharePreferce.getInstance(RemindActivity.this).setCache("isOpen",isOpen);
                }

                break;
            case R.id.iv_back:
                finish();
                break;

        }
    }

}